<template>
  <div class="ele-body">
    <el-card shadow="never">

      <!-- 搜索表单 -->
      <el-popover ref="searchRule" placement="bottom" offset="0" @hide="clearForm" :width="300">
        <template>
          <el-form :model="table.where" label-width="77px" class="ele-form-search"
                   @keyup.enter.native="$refs.ruleTable.reload()" @submit.native.prevent>
            <el-row>
              <el-col>
                <el-form-item label="考勤规则名称:">
                  <el-input v-model="table.where.name" placeholder="请输入考勤规则名称" clearable/>
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="是否节假日自动排休:">
                  <el-input v-model="table.where.disable_holiday" placeholder="请输入是否节假日自动排休" clearable/>
                </el-form-item>
              </el-col>
              <el-col>
                <div class="ele-form-actions">
                  <el-button type="primary" @click="$refs.ruleTable.reload()" icon="el-icon-search" class="ele-btn-icon">查询
                  </el-button>
                  <el-button @click="(table.where={})&&$refs.ruleTable.reload()">重置</el-button>
                </div>
              </el-col>
            </el-row>
          </el-form>
        </template>
      </el-popover>

      <!-- 操作按钮 -->
      <div class="ele-table-tool ele-table-tool-default">
        <el-button v-popover:searchRule type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">查询</el-button>
        <el-button @click="showEdit=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">新增考勤</el-button>
        <el-button @click="remove()" type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small">批量删除</el-button>
      </div>

      <!-- 数据表格 -->
      <ele-data-table ref="ruleTable" :config="table" :choose.sync="choose" height="calc(100vh - 315px)" highlight-current-row>
        <template slot-scope="{index}">
          <el-table-column type="selection" width="45" align="center" fixed="left"/>
          <el-table-column type="index" :index="index" label="编号" width="60" align="center" fixed="left" show-overflow-tooltip/>
                  
          <el-table-column prop="name" label="考勤规则名称" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="disable_holiday" label="是否节假日自动排休" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="exclude_date" label="指定不需要打卡日期，逗号分隔" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="sort" label="排序" sortable="custom" show-overflow-tooltip min-width="120"/>
                  
          <el-table-column label="创建时间" sortable="custom" show-overflow-tooltip min-width="160">
            <template slot-scope="{row}">{{ row.create_time*1000 | toDateString }}</template>
          </el-table-column>
          <el-table-column label="更新时间" sortable="custom" show-overflow-tooltip min-width="160">
            <template slot-scope="{row}">{{ row.update_time*1000 | toDateString }}</template>
          </el-table-column>
          <el-table-column label="操作" width="130px" align="center" :resizable="false"  fixed="right">
            <template slot-scope="{row}">
              <el-link @click="edit(row)" icon="el-icon-edit" type="primary" :underline="false">修改</el-link>
              <el-popconfirm title="确定要删除此考勤规则吗？" @confirm="remove(row)" class="ele-action">
                <el-link slot="reference" icon="el-icon-delete" type="danger" :underline="false">删除</el-link>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </ele-data-table>
    </el-card>

    <!-- 编辑弹窗 -->
    <el-dialog :title="editForm.id?'修改考勤规则':'新增考勤规则'" :visible.sync="showEdit" width="650px"
               @closed="editForm={}" :destroy-on-close="true" :lock-scroll="false">
      <el-form :model="editForm" ref="editRuleForm" :rules="editRules" label-width="100px">
                          
        <el-form-item label="排班名称：" prop="name">
            <el-input v-model="editForm.name" placeholder="请输入排班名称" clearable/>
        </el-form-item>

        <el-form-item label="指定对象" prop="group">
          <el-select v-model="editForm.group" placeholder="请选择" style="width:100%">
            <el-option-group
                v-for="group in attendGroups"
                :key="group.name"
                :label="group.name">
              <el-option
                  v-for="item in group.classes"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>

        <el-form-item label="工作日设置" prop="classes">
          <span>快捷设置班次:</span>&nbsp;&nbsp;<el-link type="primary" :underline="false">更改班次</el-link>

          <el-table
              :data="classTableData"
              style="width: 100%">
            <el-table-column
                label="工作日"
                width="180">
              <template slot-scope="scope">
                <span v-if="scope.$index === 0">周一</span>
                <span v-if="scope.$index === 1">周二</span>
                <span v-if="scope.$index === 2">周三</span>
                <span v-if="scope.$index === 3">周四</span>
                <span v-if="scope.$index === 4">周五</span>
                <span v-if="scope.$index === 5">周六</span>
                <span v-if="scope.$index === 6">周日</span>
              </template>
            </el-table-column>
            <el-table-column
                label="班次时间段"
                width="180">
              <template slot-scope="scope">
                <span>{{ scope.row.period_time }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                    size="mini"
                    @click="handleEdit(scope.$index, scope.row)" type="primary" plain>更改班次</el-button>
              </template>
            </el-table-column>
          </el-table>

        </el-form-item>

        <el-form-item>
          <el-button @click="includeDateDialogVisible=true">增加日期</el-button> &nbsp;&nbsp;可指定某个周末或节假日里需要打卡
        </el-form-item>

        <el-form-item>
          <el-button @click="excludeDateDialogVisible=true">排除日期</el-button> &nbsp;&nbsp;可指定某个日期里不需要打卡
        </el-form-item>
                                              
        <el-form-item label="" prop="disable_holiday">
          <el-checkbox v-model="editForm.disable_holiday">法定节假日自动排休</el-checkbox>
        </el-form-item>
                              
      </el-form>
      <div slot="footer">
        <el-button @click="showEdit=false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>

    <!--增加日期弹窗-->
    <el-dialog title="增加日期" :visible.sync="includeDateDialogVisible" width="500px">
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="增加日期：" >
          <el-date-picker
              v-model="editForm.add_date"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <el-table
          ref="multipleTable"
          :data="classData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            label="班次名称"
            width="120">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column
            prop="period"
            label="考勤时段"
            show-overflow-tooltip>
        </el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="includeDateDialogVisible=false">取消</el-button>
        <el-button type="primary" @click="includeDateDialogVisible=false">保存</el-button>
      </div>
    </el-dialog>

    <!--排除日期弹窗-->
    <el-dialog title="排除日期" :visible.sync="excludeDateDialogVisible" width="500px">
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="排除日期：" >
          <el-date-picker
              v-model="editForm.add_date"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="excludeDateDialogVisible=false">取消</el-button>
        <el-button type="primary" @click="excludeDateDialogVisible=false">保存</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "SysAttendRule",
  data() {
    return {
      table: {url: '/attendrule/list', where: {}},  // 表格配置
      choose: [],  // 表格选中数据
      showEdit: false,  // 是否显示表单弹窗
      includeDateDialogVisible: false, // 是否显示增加日期弹窗
      excludeDateDialogVisible: false, // 是否显示排除日期弹窗
      editForm: {},  // 表单数据
      attendGroups: [], // 考勤分组
      classData: [], // 所有班次信息
      classTableData: [
        {
          period_time: '2016-05-02',
          id: '1',
        }, {
          period_time: '2016-05-04',
          id: '2',
        }, {
          period_time: '2016-05-01',
          id: '3',
        }, {
          period_time: '2016-05-03',
          id: '4',
        }, {
          period_time: '2016-05-03',
          id: '5',
        }, {
          period_time: '2016-05-03',
          id: '6',
        }, {
          period_time: '2016-05-03',
          id: '7',
        }
      ], // 班次表数据
      editRules: {  // 表单验证规则
                        
        name: [
          {required: true, message: '请输入考勤规则名称', trigger: 'blur'}
        ],
                          
        exclude_date: [
          {required: true, message: '请输入指定不需要打卡日期，逗号分隔', trigger: 'blur'}
        ]
              
      },
    }
  },
  mounted() {
  },
  methods: {
    /**
     * 显示编辑
     */
    edit(row) {
      this.$http.get('/attendrule/info?id=' + row.id).then(res => {
        this.editForm = res.data;
        this.showEdit = true;
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 保存编辑
     */
    save() {
      this.$refs['editRuleForm'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({lock: true});
          this.$http.post('/attendRule/edit', this.editForm).then(res => {
            loading.close();
            this.showEdit = false;
            this.$message({type: 'success', message: res.msg});
            this.$refs.ruleTable.reload();
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /**
     * 刪除(批量刪除)
     */
    remove(row) {
      console.log(row)
      if (!row) {  // 批量删除
        if (this.choose.length === 0) return this.$message.error('请至少选择一条数据');
        let ids = this.choose.map(d => d.id);
        this.$confirm('确定要删除选中的考勤规则吗?', '提示', {type: 'warning'}).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.post('/attendRule/destroy', {id: ids}).then(res => {
            loading.close();
            this.$message({type: 'success', message: res.msg});
            this.$refs.ruleTable.reload();
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => 0);
      } else {  // 单个删除
        const loading = this.$loading({lock: true});
        this.$http.post('/attendRule/destroy', {id:row.id}).then(res => {
          loading.close();
          this.$message({type: 'success', message: res.msg});
          this.$refs.ruleTable.reload();
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }
    },
    handleEdit(index, row) {
      // 只要修改就把班次索引和工作日映射信息写入 editForm 中，默认是休息班次id
      console.log(index, row);
    },
    handleSelectionChange() { // 增加日期选择班次的时候会回调

    },
    /**
     * 清理查询表单
     */
    clearForm() {
      this.where = {};
    },
  }
}
</script>

<style scoped>
.ele-block >>> .el-upload, .ele-block >>> .el-upload-dragger {
  width: 100%;
}
</style>